<template>
  <div class="container">
    <div class="box">
      <img src="../../layout/Logo/img/logo.png" alt="Logo" />
      <h1>工艺品商城管理系统</h1>
      <div style="margin: 0.5rem">🐻</div>
      <el-form ref="form" :model="values" :rules="rules" status-icon>
        <el-form-item prop="username">
          <el-input
            v-model="values.username"
            placeholder="用户名"
            autocomplete="off"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="values.password"
            type="password"
            placeholder="密码"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submit">登 录</el-button>
        </el-form-item>
      </el-form>
      <el-alert
        class="gitee"
        title="工艺品商城的管理系统，这是一个团队项目禁止商务用途"
        type="error"
        :closable="false"
      ></el-alert>
      <el-alert
        class="gitee"
        title="测试账号：admin，密码：123456"
        type="error"
        :closable="false"
      ></el-alert>
      <el-alert
        class="gitee"
        title="git地址：https://gitee.com/liang-youhui/yxlp.bf-admin.git"
        type="success"
        :closable="false"
      ></el-alert>
    </div>
  </div>
</template>

<script>
import { defineComponent, ref, reactive } from "vue";
import { useStore } from "vuex";
import { useRouter, useRoute } from "vue-router";
import { ElMessage } from "element-plus";

import { loginApi } from "@/api/admin";

export default defineComponent({
  setup() {
    const store = useStore();
    const router = useRouter();
    const route = useRoute();

    const values = reactive({
        username: "",
        password: "",
      }),
      rules = {
        username: [
          {
            required: true,
            message: "请输入用户名",
            trigger: "blur",
          },
          {
            min: 2,
            max: 20,
            message: "用户名必须在2到20个字符之间",
            trigger: "blur",
          },
        ],
        password: [
          {
            required: true,
            message: "请输入密码",
            trigger: "blur",
          },
          {
            min: 6,
            max: 20,
            message: "用户密码必须在6到20个字符之间",
            trigger: "blur",
          },
        ],
      },
      form = ref(null);
    const submit = async () => {
      try {
        await form.value.validate();
        let { data } = await loginApi(values);
        if (data.success) {
          store.commit("/admin/login", data.user);
          router.push(route.query.redirect || "/");
          ElMessage.success(data.message+'欢迎您，来自'+returnCitySN['cname']+'的'+data.adminId.username);
        } else {
          if (data.message) {
            ElMessage.error(data.message);
          }
        }
      } catch (e) {
        console.log(e);
      }
    };

    return {
      form,
      values,
      rules,
      submit,
    };
  },
});
</script>

<style lang="scss" scoped>
.gitee {
  margin-top: 10px;
}
img {
  display: none;
}
.container {
  position: relative;
  width: 100vw;
  height: 100vh;
  background: url("../../assets/images/bj.jpg") no-repeat;
  background-size: cover;
  .box {
    width: 500px;
    position: absolute;
    padding: 30px;
    left: 50%;
    top: 50%;
    background: white;
    border-radius: 8px;
    transform: translate(-50%, -50%);
    height: 440px;
    overflow: hidden;
    box-shadow: 0 6px 20px 5px rgba(152, 152, 152, 0.1),
      0 16px 24px 2px rgba(117, 117, 117, 0.14);
    h1 {
      margin-top: 70px;
      margin-bottom: 30px;
      text-align: center;
    }
    .form {
      width: 80%;
      margin: 50px auto 15px;
      .el-input {
        margin-bottom: 20px;
      }
      .password-icon {
        cursor: pointer;
        color: #409eff;
      }
    }
    .fixed-top-right {
      position: absolute;
      top: 10px;
      right: 10px;
    }
  }
}
@media screen and (max-width: 600px) {
  .gitee {
    display: none;
  }
  img {
    display: block;
  }
  .container .box {
    background-color: #26405a;
    margin: 0;
    padding: 0;
    width: 100vw;
    height: 100vh;
    left: 0;
    top: 0;
    transform: none;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
  }
  h1 {
    color: #fff;
  }
}
</style>
